from django.core.management.base import BaseCommand
from Home.models import Department, Doctor


class Command(BaseCommand):
    help = '添加测试用的科室和医生数据'

    def handle(self, *args, **options):
        self.stdout.write('开始添加测试数据...')
        
        # 添加科室数据
        departments_data = [
            {
                'name': '内科',
                'icon': 'icon-internal',
                'description': '内科疾病诊断与治疗，包括消化系统、呼吸系统、心血管系统等疾病'
            },
            {
                'name': '外科',
                'icon': 'icon-surgery',
                'description': '外科手术及各类外科疾病的诊断治疗'
            },
            {
                'name': '妇产科',
                'icon': 'icon-gynecology',
                'description': '妇女疾病、孕产妇保健及分娩相关医疗服务'
            },
            {
                'name': '儿科',
                'icon': 'icon-pediatrics',
                'description': '儿童疾病的预防、诊断和治疗'
            },
            {
                'name': '骨科',
                'icon': 'icon-orthopedics',
                'description': '骨骼、关节、肌肉系统疾病的诊断和治疗'
            }
        ]
        
        created_departments = []
        for dept_data in departments_data:
            department, created = Department.objects.get_or_create(
                name=dept_data['name'],
                defaults={
                    'icon': dept_data['icon'],
                    'description': dept_data['description'],
                    'is_active': True
                }
            )
            created_departments.append(department)
            if created:
                self.stdout.write(f'✓ 创建科室: {department.name}')
            else:
                self.stdout.write(f'• 科室已存在: {department.name}')
        
        # 添加医生数据
        doctors_data = [
            # 内科医生
            {
                'name': '张伟医生',
                'department': '内科',
                'title': 'chief',
                'specialties': ['高血压', '糖尿病', '心脏病'],
                'experience_years': 15,
                'education': '北京医科大学医学博士',
                'hospital': '京东医疗中心',
                'consultation_fee': 200.00,
                'introduction': '从事内科临床工作15年，擅长心血管疾病和内分泌疾病的诊治',
                'rating': 4.8,
                'rating_count': 156,
                'is_featured': True
            },
            {
                'name': '李明华',
                'department': '内科',
                'title': 'attending',
                'specialties': ['呼吸系统疾病', '感冒发烧'],
                'experience_years': 8,
                'education': '上海医科大学临床医学硕士',
                'hospital': '京东医疗中心',
                'consultation_fee': 150.00,
                'introduction': '专注于呼吸系统疾病的诊断和治疗',
                'rating': 4.6,
                'rating_count': 89
            },
            # 外科医生
            {
                'name': '王建国',
                'department': '外科',
                'title': 'chief',
                'specialties': ['普通外科', '微创手术'],
                'experience_years': 20,
                'education': '协和医科大学外科学博士',
                'hospital': '京东医疗中心',
                'consultation_fee': 300.00,
                'introduction': '在普通外科和微创手术方面有丰富经验',
                'rating': 4.9,
                'rating_count': 203,
                'is_featured': True
            },
            # 妇产科医生
            {
                'name': '陈美丽',
                'department': '妇产科',
                'title': 'associate',
                'specialties': ['妇科疾病', '产前检查'],
                'experience_years': 12,
                'education': '首都医科大学妇产科学硕士',
                'hospital': '京东医疗中心',
                'consultation_fee': 180.00,
                'introduction': '专业从事妇产科临床工作，经验丰富',
                'rating': 4.7,
                'rating_count': 134
            },
            # 儿科医生
            {
                'name': '刘小华',
                'department': '儿科',
                'title': 'attending',
                'specialties': ['小儿感冒', '儿童发育'],
                'experience_years': 6,
                'education': '北京儿童医院儿科学硕士',
                'hospital': '京东医疗中心',
                'consultation_fee': 120.00,
                'introduction': '专注于儿童常见疾病的诊断和治疗',
                'rating': 4.5,
                'rating_count': 67
            },
            # 骨科医生
            {
                'name': '赵强',
                'department': '骨科',
                'title': 'chief',
                'specialties': ['骨折治疗', '关节疾病'],
                'experience_years': 18,
                'education': '北京大学骨科学博士',
                'hospital': '京东医疗中心',
                'consultation_fee': 250.00,
                'introduction': '在骨科疾病诊治方面有深厚造诣',
                'rating': 4.8,
                'rating_count': 178,
                'is_featured': True
            }
        ]
        
        for doctor_data in doctors_data:
            try:
                department = Department.objects.get(name=doctor_data['department'])
                doctor, created = Doctor.objects.get_or_create(
                    name=doctor_data['name'],
                    department=department,
                    defaults={
                        'title': doctor_data['title'],
                        'specialties': doctor_data['specialties'],
                        'experience_years': doctor_data['experience_years'],
                        'education': doctor_data['education'],
                        'hospital': doctor_data['hospital'],
                        'consultation_fee': doctor_data['consultation_fee'],
                        'introduction': doctor_data['introduction'],
                        'rating': doctor_data['rating'],
                        'rating_count': doctor_data['rating_count'],
                        'is_featured': doctor_data.get('is_featured', False),
                        'status': 'active'
                    }
                )
                if created:
                    self.stdout.write(f'✓ 创建医生: {doctor.name} ({department.name})')
                else:
                    self.stdout.write(f'• 医生已存在: {doctor.name} ({department.name})')
            except Department.DoesNotExist:
                self.stdout.write(f'✗ 科室不存在: {doctor_data["department"]}')
        
        self.stdout.write('\n测试数据添加完成！')
        self.stdout.write(f'科室总数: {Department.objects.count()}')
        self.stdout.write(f'医生总数: {Doctor.objects.count()}')